<template>
	<view class="personal-data">
		<view class="personal-data-header u-flex">
			<view class="personal-data-header-item u-flex" :class="page==1?'act':''">
				<view class="personal-data-header-item-img">
					1
				</view>
				<view class="personal-data-header-item-title">
					资料
				</view>
			</view>
			<view class="personal-data-header-item u-flex" :class="page==2?'act':''">
				<view class="personal-data-header-item-img">
					2
				</view>
				<view class="personal-data-header-item-title">
					标签
				</view>
			</view>
		</view>
		<view class="personal-data-left" v-if="page==1">
			<view class="personal-data-left-img">
				<view class="personal-data-left-img-ava" @click="uploads()">
					<image
						:src="inputVal.avatar ? inputVal.avatar : 'https://zaiyiqi-1322608024.cos.ap-nanjing.myqcloud.com/static/img/mine/personal/ava-def.png'"
						mode="aspectFill"></image>
				</view>
				<view class="personal-data-left-img-camera">
					<image
						src="https://zaiyiqi-1322608024.cos.ap-nanjing.myqcloud.com/static/img/mine/personal/choose-img.png"
						mode=""></image>
				</view>
			</view>
			<view class="personal-data-left-list">
				<mine-personalitem title="昵称" @clickArrow="clickArrow('name')" :showArrow="false" placeholder="请输入"
					:enableInput="true" @change="changeInput($event,'name')">
				</mine-personalitem>
				<mine-personalitem title="性别" @clickArrow="clickArrow('sex')" :value="sex" :showArrow="true"
					placeholder="请选择" :enableInput="false">
				</mine-personalitem>
				<mine-personalitem title="年龄" @clickArrow="clickArrow('age')" :value="age" :showArrow="true"
					placeholder="请选择" :enableInput="false">
				</mine-personalitem>
				<mine-personalitem title="现居地" @clickArrow="clickArrow('city')" :value="now_home || ''"
					:showArrow="true" placeholder="请选择" :enableInput="false">
				</mine-personalitem>
				<mine-personalitem title="家乡" @clickArrow="clickArrow('province')" :value="home" :showArrow="true"
					placeholder="请选择" :enableInput="false">
				</mine-personalitem>
				<mine-personalitem title="职业" @clickArrow="clickArrow('career')" :value="inputVal.career || ''"
					:showArrow="true" placeholder="请选择" :enableInput="false">
				</mine-personalitem>
				<mine-personalitem title="年收入" @clickArrow="clickArrow('income')" :showArrow="false" placeholder="请输入"
					:enableInput="true" @change="changeInput($event,'income')">
				</mine-personalitem>
			</view>
			<view class="personal-data-left-warm">
				为了打造100%真实的交友平台，个人资料请如实填写，不真实不走心的资料审核时将会被拒接
			</view>
			<view class="personal-data-left-btn public-btn" style="margin: 0 auto;" @click="nextPage()">
				下一步
			</view>
		</view>
		<view class="personal-data-right" v-if="page==2">
			<view class="personal-data-right-nav u-flex">
				<view class="personal-data-right-nav-item" @click="addNavId(item)"
					:class="(chooseNavId == item.id )?'act':''" v-for="(item,index) in navList" :key="index">
					{{item.name}}
				</view>
			</view>
			<view class="personal-data-right-list u-flex">
				<view class="personal-data-right-list-item" @click="addIds(item)"
					:class="(chooseIds.findIndex(d=>d==item.id) != -1)?'act':''" v-for="(item,index) in list"
					:key="index">
					{{item.name}}
				</view>
			</view>
			<view class="personal-data-right-fresh u-flex u-text-center u-row-center">
				<image src="https://zaiyiqi-1322608024.cos.ap-nanjing.myqcloud.com/static/img/mine/personal/refresh.png"
					mode=""></image>
				<view class="">
					换一批
				</view>
			</view>
			<view class="personal-data-right-fresh" style="text-align: center;">
				{{chooseIds.length}}/20
			</view>
			<view class="public-btn public-btn-color" @click="confirm" style="margin: 0 auto;margin-bottom: 60rpx;">
				保存
			</view>
		</view>

		<!-- 性别 -->
		<u-picker mode="selector" v-model="sexshow" :range="sexList" range-key="label" :default-selector="cityIndex"
			@confirm="changeSelect('sex',$event)">
		</u-picker>
		<!-- 年龄 -->
		<u-picker mode="selector" v-model="ageshow" :range="ageList" range-key="label" :default-selector="cityIndex"
			@confirm="changeSelect('age',$event)">
		</u-picker>
		<!-- 现居地 -->
		<u-picker mode="selector" v-model="cityshow" :range="cityList" range-key="name" :default-selector="cityIndex"
			@columnchange="columnchange" @confirm="changeSelect('city',$event)">
		</u-picker>
		<!-- 家乡 -->
		<u-picker mode="selector" v-model="provinceshow" :range="provinceList" range-key="name"
			:default-selector="provinceIndex" @columnchange="columnchange" @confirm="changeSelect('province',$event)">
		</u-picker>
		<!-- 职业 -->
		<u-picker mode="selector" v-model="careershow" :range="careerList" range-key="name"
			:default-selector="careerIndex" @columnchange="columnchange" @confirm="changeSelect('career',$event)">
		</u-picker>

	</view>
</template>

<script>
	import {
		upload
	} from '@/utils/upload.js'
	//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
	import minePersonalitem from '../../../components/mine-personalitem/mine-personalitem.vue'
	export default {
		// 组件名称
		name: 'demo',
		// 组件参数 接收来自父组件的数据
		props: {},
		// 局部注册的组件,import引入的组件需要注入到对象中才能使用
		components: {
			minePersonalitem
		},
		// 组件状态值
		data() {
			return {
				page: 1, //1资料 2标签
				inputVal: {
					avatar: '', // 头像
					sex: '', // 性别
					age: '', // 年龄
					home: '', // 家乡
					now_home: '', // 现居地
					career: '', // 职业
					income: '', // 年收入
				},
				navList: [],
				chooseNavId: 1,
				list: [],
				chooseIds: [],
				sexshow: false,
				ageshow: false,
				cityshow: false,
				provinceshow: false,
				careershow: false,
				sexList: [{
						value: 0,
						label: '女'
					},
					{
						value: 1,
						label: '男'
					}
				],
				provinceList: [],
				cityList: [],
				careerList: [],
				sexIndex: 0,
				ageIndex: 0,
				province: '',
				city: '',
				nickname: '',
				sex: '',
				age: '',
				home: '',
				now_home: '',
				career: '',
				cityIndex: [],
				provinceIndex: [],
				careerIndex: [],
				location: {
					lat: '',
					lng: ''
				},
				ageList: [{
					value: 18,
					label: '18岁'
				}, {
					value: 19,
					label: '19岁'
				}, {
					value: 20,
					label: '20岁'
				}, {
					value: 21,
					label: '21岁'
				}, {
					value: 22,
					label: '22岁'
				}, {
					value: 23,
					label: '23岁'
				}, {
					value: 24,
					label: '24岁'
				}, {
					value: 25,
					label: '25岁'
				}, {
					value: 26,
					label: '26岁'
				}, {
					value: 27,
					label: '27岁'
				}, {
					value: 28,
					label: '28岁'
				}, {
					value: 29,
					label: '29岁'
				}, {
					value: 30,
					label: '30岁'
				}, {
					value: 31,
					label: '31岁'
				}, {
					value: 32,
					label: '32岁'
				}, {
					value: 33,
					label: '33岁'
				}, {
					value: 34,
					label: '34岁'
				}, {
					value: 35,
					label: '35岁'
				}, {
					value: 36,
					label: '36岁'
				}, {
					value: 37,
					label: '37岁'
				}, {
					value: 38,
					label: '38岁'
				}, {
					value: 39,
					label: '39岁'
				}, {
					value: 40,
					label: '40岁'
				}, {
					value: 41,
					label: '41岁'
				}, {
					value: 42,
					label: '42岁'
				}, {
					value: 43,
					label: '43岁'
				}, {
					value: 44,
					label: '44岁'
				}, {
					value: 45,
					label: '45岁'
				}, {
					value: 46,
					label: '46岁'
				}, {
					value: 47,
					label: '47岁'
				}, {
					value: 48,
					label: '48岁'
				}, {
					value: 49,
					label: '49岁'
				}, {
					value: 50,
					label: '50岁'
				}, {
					value: 51,
					label: '51岁'
				}, {
					value: 52,
					label: '52岁'
				}, {
					value: 53,
					label: '53岁'
				}, {
					value: 54,
					label: '54岁'
				}, {
					value: 55,
					label: '55岁'
				}, {
					value: 56,
					label: '56岁'
				}, {
					value: 57,
					label: '57岁'
				}, {
					value: 58,
					label: '58岁'
				}, {
					value: 59,
					label: '59岁'
				}, {
					value: 60,
					label: '60岁'
				}],
			}
		},
		// 计算属性
		computed: {},
		// 侦听器
		watch: {},
		created() {
			// this.getInfo()
			this.getLabel() // 标签列表
			this.getProvince() // 获取省份
		},
		// 组件方法
		methods: {


			// 选择器
			changeSelect(type, e) {
				console.log('asldkjlkj', e)
				if (type == 'sex') {
					this.sexIndex = (e[0] == undefined ? 0 : e[0])
					this.sexList.filter((item, i) => {
						if (i == this.sexIndex) {
							this.sex = item.label
							this.inputVal.sex = item.value
						}
					})
				} else if (type == 'age') {
					this.ageIndex = (e[0] == undefined ? 0 : e[0])
					this.ageList.filter((item, i) => {
						if (i == this.ageIndex) {
							this.age = item.label
							this.inputVal.age = item.value
						}
					})
				} else if (type == 'province') {
					this.provinceIndex = (e[0] == undefined ? 0 : e[0])
					this.provinceList.filter((item, i) => {
						if (i == this.provinceIndex) {
							// this.provinceIndex = item.label
							this.home = item.name
							this.inputVal.home = item.id
						}
					})
				} else if (type == 'city') {
					this.cityIndex = (e[0] == undefined ? 0 : e[0])
					this.cityList.filter((item, i) => {
						if (i == this.cityIndex) {
							// this.cityIndex = item.label
							this.now_home = item.name
							this.inputVal.now_home = item.id
						}
					})
				} else if (type == 'career') {
					this.careerIndex = (e[0] == undefined ? 0 : e[0])
					this.careerList.filter((item, i) => {
						if (i == this.careerIndex) {
							this.career = item.name
							this.inputVal.career = item.id
						}
					})
				}

				this.$forceUpdate()
			},
			// 完成
			confirm(type) {
				// 下一步
				this.$http('user.information', this.inputVal).then(res => {
					console.log(res, 'resasldkjlaksjd')
					this.$utils.toast(res.info)
					setTimeout(() => {
						uni.navigateBack({
							delta: 1
						})
					}, 1000)
				})
			},
			// 弹出选择弹窗
			clickArrow(type) {
				if (type == 'sex') {
					this.sexshow = true
				} else if (type == 'age') {
					this.ageshow = true
				} else if (type == 'city') {
					this.cityshow = true
				} else if (type == 'province') {
					this.provinceshow = true
				} else if (type == 'career') {
					this.careershow = true
				}
			},
			// 输入框
			changeInput(e, type) {
				this.inputVal[type] = e
			},
			// 获取会员资料详情
			getInfo() {
				this.$http('user.informationInfo', {
					token: uni.getStorageSync('token')
				}, '').then(res => {
					console.log(res, 'informationInfo')
					this.inputVal = res.data
					this.sexList.filter(e => {
						if (e.value == res.data.sex) {
							this.sex = e.label
						}
					})
					this.provinceList.filter((item, index) => {
						// console.log(item,index,res.data.name)
						if (index === res.data.home) {
							// console.log(item, index, 'cityList城市列表')
							this.provinceIndex = [index]
							this.home = item.name
							this.inputVal.home = item.id
						}
					})
					this.cityList.filter((item, index) => {
						// console.log(item,index,res.data.name)
						if (index === res.data.now_home) {
							// console.log(item, index, 'cityList城市列表')
							this.cityIndex = [index]
							this.now_home = item.name
							this.inputVal.now_home = item.id
						}
					})
				})
			},
			//获取当前位置
			getLocation() {
				let location = {
					lat: 0,
					lng: 0,
				}
				// console.log(location, 'location')
				let that = this
				uni.getLocation({
					success(res) {
						// console.log(res, 'res')
						location.lat = res.latitude
						location.lng = res.longitude
						// console.log(res.latitude, res.longitude, 'res.latitude,res.longituderes.latitude,res.longitude')
						// 根据经纬度获取城市
						uni.request({
							url: 'https://apis.map.qq.com/ws/geocoder/v1/',
							data: {
								location: `${res.latitude},${res.longitude}`,
								key: 'LNKBZ-7K2K4-FBTUF-F4EDL-DL3CH-T4FDB'
							},
							success(res) {
								console.log(res, 'res', 'https://apis.map.qq.com/ws/geocoder/v1/')
								// that.city = res.data.result.ad_info.city
								that.provinceList.filter(item => {
									// console.log(item.name, res.data.result.ad_info.province, 'item.name, res.data.result.ad_info.city')
									if (item.name === res.data.result.ad_info.province) {
										that.city = res.data.result.ad_info.city
										that.province = res.data.result.ad_info.province
										// that.getArea(item)
										that.getCity(item)
									}
								})
							}
						})
						that.location = location
					},
					fail(err) {
						console.log(err, 'err')
						that.location = location
						// that.getList()
						if (err.errMsg === 'getLocation:fail 频繁调用会增加电量损耗，可考虑使用 wx.onLocationChange 监听地理位置变化') {
							uni.showToast({
								title: '请勿频繁定位',
								icon: 'none'
							})
						}
						if (err.errMsg === 'getLocation:fail auth deny') {
							// 未授权
							uni.showToast({
								title: '无法定位，请重新获取位置信息',
								icon: 'none'
							})
							authDenyCb && authDenyCb()
							self.isLocated = false
						}
						if (err.errMsg === 'getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF') {
							uni.showModal({
								content: '请开启手机定位服务',
								showCancel: false
							})
						}
					}
				})
			},
			// 获取省
			getProvince() {
				this.$http('config.area', {}, '').then(res => {
					// console.log(res, 'res')
					this.provinceList = res.data
					this.getLocation() // 获取当前位置
				})
			},
			// 获取地区
			getCity(item) {
				this.$http('config.area', {
					pid: item.id
				}, '').then(res => {
					// console.log(res, 'res')
					this.cityList = res.data
					this.cityList.unshift({
						id: '',
						name: '不限'
					})
					this.cityList.filter((item, index) => {
						if (item.name === this.city) {
							this.cityIndex = [index]
							// this.getArea(item)
						}
					})
					this.getInfo()
				})
			},
			// 上传图片
			uploads() {
				// console.log(12222)
				upload({
					count: 1,
					success: (res) => {
						console.log(res, 'resresres')
						this.inputVal.avatar = res
						console.log(this.inputVal.avatar);
						this.$forceUpdate()
					}
				})
			},
			// 标签列表
			getLabel() {
				this.$http('config.label', {}, '').then(res => {
					console.log(res, 'res');
					this.chooseNavId = res.data[0].id
					this.navList = res.data
					this.getZLabel()
				})
			},
			// 子标签
			getZLabel() {
				this.$http('config.label', {
					pid: this.chooseNavId
				}, '').then(res => {
					console.log(res, 'res');
					this.list = res.data
				})
			},
			nextPage() {
				this.page = 2
			},
			addIds(item) {
				let index = this.chooseIds.findIndex(d =>
					d == item.id
				)
				console.log(index)
				if (index == -1) {
					this.chooseIds.push(item.id)
				} else {
					this.chooseIds.splice(index, 1)
				}
				console.log(this.chooseIds)
			},
			addNavId(item) {
				this.chooseNavId = item.id
				this.getZLabel()
			}
		}
	}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<!--使用了scoped属性之后，父组件的style样式将不会渗透到子组件中，-->
<!--然而子组件的根节点元素会同时被设置了scoped的父css样式和设置了scoped的子css样式影响，-->
<style scoped lang="scss">
	.personal-data {
		&-header {
			margin-top: 60rpx;
			margin-bottom: 60rpx;
			height: 40rpx;
			justify-content: center;

			.act {
				.personal-data-header-item-img {
					background: #7266FFFF;
				}

				.personal-data-header-item-title {
					color: #7266FFFF;
				}
			}

			&-item {
				width: 200rpx;
				justify-content: center;

				&-img {
					width: 40rpx;
					height: 40rpx;
					text-align: center;
					line-height: 40rpx;
					font-size: 24rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 500;
					color: #FFFFFF;
					background: #CDCDCD;
					border-radius: 50%;
					margin-right: 12rpx;
				}

				&-title {
					font-size: 28rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 500;
					color: #8A9199;
				}
			}
		}

		&-left {
			padding-bottom: 20rpx;

			&-img {
				position: relative;
				width: 136rpx;
				height: 136rpx;
				margin: 0 auto;

				&-ava {
					image {
						width: 136rpx;
						height: 136rpx;
						border-radius: 50%;
						display: block;
					}
				}

				&-camera {
					position: absolute;
					right: 0;
					bottom: 0;

					image {
						height: 40rpx;
						width: 40rpx;
					}
				}
			}

			&-list {
				padding: 0 64rpx;
			}

			&-warm {
				width: 610rpx;
				margin: 0 auto;
				margin: 60rpx;
				font-size: 24rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #8A9199;
				text-align: center;
			}
		}

		&-right {
			padding: 0 32rpx;

			&-nav {
				.act {
					font-size: 28rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 500;
					color: #2E3033;
				}

				.act::after {
					content: '';
					display: block;
					position: absolute;
					bottom: -6rpx;
					width: 28rpx;
					left: 50%;
					margin-left: -14rpx;
					height: 4rpx;
					background: #7266FF;
					border-radius: 2rpx;
				}

				&-item {
					margin-right: 60rpx;
					position: relative;
					font-size: 28rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					color: #8A9199;
				}
			}

			&-list {
				flex-wrap: wrap;

				&-item {
					margin: 32rpx 15rpx;
					width: 136rpx;
					height: 64rpx;
					background: #F5F5F5;
					border-radius: 34rpx;
					font-size: 28rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 500;
					color: #5C6166;
					text-align: center;
					line-height: 64rpx;
				}

				.act {
					background: #E7E5FF;
					color: #7266FF;
				}
			}

			&-fresh {
				font-size: 28rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #8A9199;
				margin: 40rpx 0;

				image {
					height: 40rpx;
					width: 40rpx;
				}
			}
		}
	}
</style>